﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.common;
using Peekaboo2008.model;
using Peekaboo2008.dao;

namespace Peekaboo2008.view
{
    public partial class FormChiTietCongNo : Form
    {
        private FormCongNo frmCongNo = null;
        private CongNoModel congNoModel;

        public FormChiTietCongNo(CongNoModel _congNoModel, ref FormCongNo _frmCongNo)
        {
            InitializeComponent();

            FormChiTietCongNoLoad();
            frmCongNo = _frmCongNo;


            congNoModel = _congNoModel;

            TxtTyGia.Text = congNoModel.Ty_gia;
            txtThanhToan.Text = congNoModel.Thanh_toan;
            txtThanhTien.Text = congNoModel.Thanh_tien;
            txtGhiChu.Text = congNoModel.Ghi_chu;
            cbbNgoaiTe.SelectedValue = congNoModel.Ma_ngoai_te;
            cbbKH.SelectedValue = congNoModel.Ma_khach_hang;
            cbbLoaiCongNo.SelectedValue = congNoModel.Ma_loai_cong_no;
            
        }

        public FormChiTietCongNo(ref FormCongNo _frmCongNo)
        {
            InitializeComponent();
            FormChiTietCongNoLoad();
            frmCongNo = _frmCongNo;
        }

        public void FormChiTietCongNoLoad()
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormChiTietCongNo_KeyDown);

            DataTable dtKH = KhachHangDAO.KhachHangMuaHang_GetAll();
            cbbKH.DataSource = dtKH;
            cbbKH.ValueMember = "ID";            
            cbbKH.DisplayMember = "Ten";
            cbbKH.SelectedIndex = 0;

            DataTable dtLoaiCongNo = CongNoDAO.LoaiCongNo_GetAll();
            cbbLoaiCongNo.DataSource = dtLoaiCongNo;
            cbbLoaiCongNo.ValueMember = "ID";
            cbbLoaiCongNo.DisplayMember = "Ten";
            cbbLoaiCongNo.SelectedIndex = 0;

            DataTable dtNgoaiTe = NgoaiTeDAO.getAll();
            cbbNgoaiTe.DataSource = dtNgoaiTe;
            cbbNgoaiTe.ValueMember = "ID";
            cbbNgoaiTe.DisplayMember = "Ki_hieu";            
        }

        private void FormChiTietCongNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                btnChapNhan.PerformClick();
            }
            if (e.KeyCode == Keys.F6)
            {
                btnThemNhanhKH.PerformClick();
            }
            if (e.KeyCode == Keys.F7)
            {
                btnThemNhanhLoaiCongNo.PerformClick();
            }
            if (e.KeyCode == Keys.F12)
            {
                this.Dispose();
            }
        }

        private void btnThemNhanhKH_Click(object sender, EventArgs e)
        {
            FormChiTietCongNo frmChiTietCongNo = this;
            FormKhachHang frmKhachHang = null;
            FormChiTietKhachHang frmChiTietKhachHang = new FormChiTietKhachHang(ref frmKhachHang);
            frmChiTietKhachHang.ShowDialog();
            FormChiTietCongNoLoad();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {         
                 if (congNoModel == null)
                 {
                        congNoModel = new CongNoModel();
                        congNoModel.Ghi_chu = txtGhiChu.Text;
                        congNoModel.Thanh_tien = txtThanhTien.Text;
                        congNoModel.Thanh_toan = txtThanhToan.Text;
                        congNoModel.Ty_gia = TxtTyGia.Text;
                        congNoModel.Ma_ngoai_te = cbbNgoaiTe.SelectedValue.ToString();
                        congNoModel.Ma_khach_hang = cbbKH.SelectedValue.ToString();
                        congNoModel.Ma_loai_cong_no = cbbLoaiCongNo.SelectedValue.ToString();

                        if (CongNoDAO.InsertCongNo(congNoModel))
                        {
                            MessageBox.Show("Thêm công nợ thành công");
                            if (frmCongNo != null)
                            {
                                frmCongNo.FormCongNoLoad();
                            }
                            this.Dispose();
                        }
                 }
                else
                {
                    congNoModel.Ghi_chu = txtGhiChu.Text;
                    congNoModel.Thanh_tien = txtThanhTien.Text;
                    congNoModel.Thanh_toan = txtThanhToan.Text;
                    congNoModel.Ty_gia = TxtTyGia.Text;
                    congNoModel.Ma_ngoai_te = cbbNgoaiTe.SelectedValue.ToString();
                    congNoModel.Ma_khach_hang = cbbKH.SelectedValue.ToString();
                    congNoModel.Ma_loai_cong_no = cbbLoaiCongNo.SelectedValue.ToString();
                    if (CongNoDAO.UpdateCongNo(congNoModel))
                    {
                        MessageBox.Show("Cập nhật công nợ thành công","Thông báo !",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        frmCongNo.FormCongNoLoad();                       
                    }
                    else
                    {
                        MessageBox.Show("Cập nhật công nợ thất bại","Thông báo !",MessageBoxButtons.OK,MessageBoxIcon.Error);                        
                        
                    }
                    this.Dispose();
                }           
        }

        private void TxtTyGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnThemNhanhLoaiCongNo_Click(object sender, EventArgs e)
        {
            FormChiTietCongNo formChiTietCongNo = this;
            FormChiTietLoaiCongNo formChiTietLoaiCongNo = new FormChiTietLoaiCongNo(ref formChiTietCongNo);
            formChiTietLoaiCongNo.ShowDialog();
        } 
    }
}
